extends CanvasLayer

var weapon_slot_scene = preload("res://Scene/UI/WeaponSlot.tscn") # 需要创建武器槽UI

#@onready var health_bar: ProgressBar = $HealthBar
@onready var experience_bar: ProgressBar = $MarginContainer/ExperienceBar
#@onready var player: CharacterBody2D = $"../World/Player"
@onready var game_timer: Label = %GameTimer

func _ready():
	# 初始化UI元素
	
	
	# Initialize experience bar
	experience_bar.max_value = 1.0
	experience_bar.value = 0.0
	
	$GameOverScreen.visible = false
	$LevelUpScreen.visible = false
	
	#player.took_damage.connect(_on_player_took_damage)
	
	# 设置升级界面
	setup_level_up_screen()
	
	# 设置游戏结束界面
	setup_game_over_screen()

#func _on_player_took_damage(amount: float, attacker: Node,current_health: float):
	#update_health_bar(current_health,player.max_health)

func setup_level_up_screen():
	# 这里可以添加升级选择UI的代码
	# 例如创建三个按钮，每个按钮代表一种升级选择
	pass

func setup_game_over_screen():
	# 添加游戏结束UI元素，如重新开始按钮等
	var restart_button = Button.new()
	restart_button.text = "重新开始"
	restart_button.position = Vector2(200, 200)
	restart_button.connect("pressed", on_restart_pressed)
	$GameOverScreen.add_child(restart_button)

func on_restart_pressed():
	# 重新开始游戏
	get_tree().paused = false
	get_tree().reload_current_scene()

#func update_health_bar(current_health, max_health):
	#health_bar.max_value = max_health
	#health_bar.value = current_health

func update_experience_bar(current_exp, exp_to_next_level):
	#experience_bar.max_value = 1.0
	if exp_to_next_level<=0:
		experience_bar.value=0
	else :
		print(str(current_exp / exp_to_next_level))
		experience_bar.value=min(current_exp / exp_to_next_level,1 ) 


func on_weapon_added(weapon_data):
	# 添加武器时更新武器槽UI
	var weapon_slot = weapon_slot_scene.instantiate()
	$WeaponSlots.add_child(weapon_slot)
	weapon_slot.update_display(weapon_data)

#func show_level_up_options(options):
	## 显示升级选项
	#$LevelUpScreen.visible = true
	## 这里添加代码，根据参数options更新升级选项的UI
	## 例如，添加三个按钮，每个按钮代表一个升级选项
#
#func on_level_up_option_selected(option_index):
	## 选择升级选项后的处理
	#$LevelUpScreen.visible = false
	#get_tree().paused = false 
	
func update_time_display(game_time):
	var minutes = int(game_time / 60)
	var seconds = int(game_time) % 60
	game_timer.text = "%02d:%02d" % [minutes, seconds]
	
	
